﻿using CloudSharingStation_UWP_.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace CloudSharingStation_UWP_
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class UploadPage : Page
    {
        private ResourceList resourceList = null;
        private StorageFile selectedFile = null;
        public UploadPage()
        {
            this.InitializeComponent();
            pgbUpload.Visibility = Visibility.Collapsed;
        }

        private async void btnChooseFile_Clicked(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            picker.FileTypeFilter.Add("*");
            picker.ViewMode = PickerViewMode.List;
            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                txtPath.Text = file.Path;
                var size =await file.GetBasicPropertiesAsync();
                txtSize.Text = size.Size.ToString()+"B";
                txtCategory.Text = file.FileType;
                selectedFile = file;
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            resourceList = (ResourceList)e.Parameter;
            txtUploader.Text = resourceList.currentUser.name;
        }

        private async void btnUpload_Clicked(object sender, RoutedEventArgs e)
        {
            if (cmbDepartment.SelectedItem==null||cmbType.SelectedItem==null||txtPath.Text==null)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "上传失败";
                dialog.Content = "请选择文件和所有需要的信息！";
                dialog.CloseButtonText = "关闭";
                var result = dialog.ShowAsync();
                return;
            }
            string department = (string)cmbDepartment.SelectedItem;
            string path = txtPath.Text;
            StudyResource studyResource = StudyResourceFactory.getStudyResource("StudyResource"); // 生产出一个studyResource
            studyResource.department = department;
            studyResource.uploaderName = resourceList.currentUser.name; // 上传者姓名
            studyResource.fileCategory = txtCategory.Text; // 获取上传文件的文件类型
            studyResource.fileSize = Double.Parse(txtSize.Text.Substring(0,txtSize.Text.Length-1)); // 获取文件大小
            studyResource.fileName = selectedFile.Name; // 获取文件名
            pgbUpload.Visibility = Visibility.Visible;
            txtUploadAlert.Text = "上传进度：上传中";
            Task<HttpStatusCode> task = Task<HttpStatusCode>.Run(() => resourceList.UploadFile(path, studyResource)); //上传文件
            await task;
            pgbUpload.Visibility = Visibility.Collapsed;
            txtUploadAlert.Text = "上传进度：上传完成";

        }
    }
}
